package com.onlyMe.world.tiling
{
	import com.flesler.componentas.core.Destroyable;
	import com.onlyMe.world.objects.IsleObject;
	import com.qb9.flashlib.utils.ArrayUtil;

	public class Tile implements Destroyable
	{
		private var _children:Array = [];
		
		private var _blocked:Boolean;
		private var _type:String;
		
		public function Tile(type:String)
		{
			_type = type;
		}
		
		public function get blocked():Boolean
		{
			return _blocked;
		}
		
		public function set blocked(v:Boolean) :void
		{
			_blocked = v;
		}
		
		public function get blockedByChildren():Boolean
		{
			for each (var object:IsleObject in _children)
				if (object.blocks)
					return true;
			return false;
		}
		
		public function get children():Array
		{
			return _children.concat();
		}
		
		public function get type():String
		{
			return _type;
		}
		
		public function set type(value:String):void
		{
			_type = value;
		}
		
		public function hasChild(obj:IsleObject):Boolean
		{
			return ArrayUtil.contains(_children, obj);
		}
		
		public function addChild(obj:IsleObject):void
		{
			ArrayUtil.removeElement(_children, obj);
			_children.push(obj);
		}
		
		public function removeChild(obj:IsleObject):void
		{
			var i:int = _children.indexOf(obj);
			if (i === -1)
				throw new Error('object is not child of this tile');
				
			_children.splice(i, 1);
		}
		
		public function activate(activator:IsleObject):void
		{
			for each (var object:IsleObject in _children)
				if (object !== activator)
					object.activate(activator);
		}
		
		public function get isDestroyed():Boolean
		{
			return _children === null;
		}
		
		public function destroy():void
		{
			_children = null;
		}
		
	}
}
